<script setup>
import { ref } from "vue";
import WangEditorView from "@/components/WangEditorView.vue";
import {addPostOne} from "@/api/post.js";
import {getUser} from "@/store/userStore.js";
import {ElMessage} from "element-plus";
import { Upload } from '@element-plus/icons-vue'

const loading = ref(false);
const form = ref({
  title: "",
  content: "",
});

const onSubmit = async () => {
  if (!form.value.title.trim()) {
    ElMessage.warning('请输入文章标题');
    return;
  }
  if (!form.value.content.trim()) {
    ElMessage.warning('请输入文章内容');
    return;
  }

  loading.value = true;
  try {
    form.value.userId = getUser().userInfo.id;
    const res = await addPostOne(form.value);
    if (res.code === 200) {
      ElMessage.success("发布成功");
      form.value.title = "";
      form.value.content = "";
    } else {
      ElMessage.error("发布失败");
    }
  } catch (error) {
    ElMessage.error("发布失败，请稍后重试");
  } finally {
    loading.value = false;
  }
};
</script>

<template>
  <div class="post-publish-container">
    <el-card class="publish-card">
      <template #header>
        <div class="card-header">
          <h2>发布文章</h2>
          <el-button type="primary" @click="onSubmit" :loading="loading">
            <el-icon><Upload /></el-icon>
            发布文章
          </el-button>
        </div>
      </template>

      <el-form :model="form" class="publish-form">
        <el-form-item>
          <el-input
            v-model="form.title"
            placeholder="请输入文章标题"
            class="title-input"
            :maxlength="100"
            show-word-limit
          />
        </el-form-item>

        <el-form-item>
          <div class="editor-container">
            <WangEditorView v-model="form.content" />
          </div>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<style scoped lang="scss">
.post-publish-container {
  padding: 20px;
  background-color: #f5f7fa;
  min-height: 100vh;

  .publish-card {
    max-width: 1000px;
    margin: 0 auto;
    border-radius: 8px;
    box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);

    .card-header {
      display: flex;
      justify-content: space-between;
      align-items: center;
      padding: 0 20px;

      h2 {
        margin: 0;
        font-size: 20px;
        color: #303133;
        font-weight: 500;
      }

      .el-button {
        display: flex;
        align-items: center;
        gap: 6px;
        padding: 12px 24px;
        font-size: 14px;
        border-radius: 6px;
        transition: all 0.3s;

        &:hover {
          transform: translateY(-1px);
          box-shadow: 0 4px 12px rgba(64, 158, 255, 0.2);
        }
      }
    }

    .publish-form {
      padding: 20px;

      .title-input {
        :deep(.el-input__wrapper) {
          box-shadow: 0 0 0 1px #dcdfe6 inset;
          transition: all 0.3s;
          padding: 12px 16px;
          font-size: 16px;
          border-radius: 8px;

          &:hover {
            box-shadow: 0 0 0 1px var(--el-color-primary) inset;
          }

          &.is-focus {
            box-shadow: 0 0 0 1px var(--el-color-primary) inset;
          }
        }

        :deep(.el-input__inner) {
          height: 24px;
          line-height: 24px;
        }
      }

      .editor-container {
        margin-top: 20px;
        border-radius: 8px;
        overflow: hidden;
        box-shadow: 0 0 0 1px #dcdfe6;
        transition: all 0.3s;

        &:hover {
          box-shadow: 0 0 0 1px var(--el-color-primary);
        }
      }
    }
  }
}

:deep(.w-e-text-container) {
  min-height: 400px !important;
  background-color: #fff;
}

:deep(.w-e-toolbar) {
  border-bottom: 1px solid #dcdfe6;
  background-color: #fafafa;
  padding: 8px 16px;
}
</style>
